package com.bbs.controller;

import com.bbs.common.Constants;
import com.bbs.common.ServiceResultEnum;
import com.bbs.entity.BBSPostComment;
import com.bbs.entity.BBSUser;
import com.bbs.service.IBBSPostCommentService;
import com.bbs.util.Result;
import com.bbs.util.ResultGenerator;
import com.bbs.util.SensitiveWordUtil;
import com.bbs.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @PackageName: com.bbs.controller
 * @ClassName: BBSPostCommentController
 * @Description:
 * @Author: Rambo
 * @Data: 2024/6/18 11:23
 */
@Controller
public class BBSPostCommentController {
    @Autowired
    private IBBSPostCommentService bbsPostCommentService;

    /**
     * 回复帖子
     * @param postId
     * @param parentCommentUserId
     * @param commentBody
     * @param verifyCode
     * @param httpSession
     * @return
     */
    @PostMapping("/replyPost")
    @ResponseBody
    public Result replyComment(@RequestParam("postId") Long postId,
                               @RequestParam(value = "parentCommentUserId", required = false) Long parentCommentUserId,
                               @RequestParam("commentBody") String commentBody,
                               @RequestParam("verifyCode") String verifyCode,
                               HttpSession httpSession) {
        // 判断postId是否为空或者postId是否合法
        if (null == postId || postId < 0) {
            return ResultGenerator.genFailResult("postId参数错误");
        }
        // 判断评论体是否为空
        if (StringUtil.isEmpty(commentBody)) {
            return ResultGenerator.genFailResult("commentBody参数错误");
        }
        // 判断评论内容是否超过200字
        // 调用 commentBody.trim() 时，它会返回一个新的字符串，该字符串是原始字符串去除前导（leading）和尾部（trailing）空白字符后的结果
        if (commentBody.trim().length() > 200) {
            return ResultGenerator.genFailResult("评论内容过长");
        }

        // 过滤评论体中的敏感词汇
        commentBody = SensitiveWordUtil.replace(commentBody);

        // 验证码校验
        String kaptchaCode = httpSession.getAttribute(Constants.VERIFY_CODE_KEY) + "";
        if (!StringUtils.hasLength(kaptchaCode) || !verifyCode.equalsIgnoreCase(kaptchaCode)) {
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR.getResult());
        }

        // 获取正在登录的用户信息
        BBSUser bbsUser = (BBSUser) httpSession.getAttribute(Constants.USER_SESSION_KEY);

        // 封装评论信息
        BBSPostComment bbsPostComment = new BBSPostComment();
        bbsPostComment.setCommentBody(commentBody);
        bbsPostComment.setCommentUserId(bbsUser.getUserId());
        bbsPostComment.setParentCommentUserId(parentCommentUserId);
        bbsPostComment.setPostId(postId);

        if (bbsPostCommentService.addPostComment(bbsPostComment)) {
            httpSession.removeAttribute(Constants.VERIFY_CODE_KEY);
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("请求失败，请检查参数及账号是否有操作权限");
        }
    }

    /**
     * 删除评论
     * @param commentId 评论id
     * @param httpSession
     * @return
     */
    @PostMapping("/delReply/{commentId}")
    @ResponseBody
    public Result delReply(@PathVariable("commentId") Long commentId,
                           HttpSession httpSession) {

        // 判断commentId是否为空或该id是否合法
        if (null == commentId || commentId < 0) {
            return ResultGenerator.genFailResult("commentId参数错误");
        }

        // 获取登陆者信息
        BBSUser bbsUser = (BBSUser) httpSession.getAttribute(Constants.USER_SESSION_KEY);

        // 调用业务层方法删除评论
        if (bbsPostCommentService.delPostComment(commentId,bbsUser.getUserId())) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("请求失败，请检查参数及账号是否有操作权限");
        }
    }
}
